package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.activity.MainActivity;
import com.mission.schedule.activity.NoteEditActivity;
import com.mission.schedule.activity.NoteListActivity;
import com.mission.schedule.adapter.NoteExpandListAdapter;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.ExpandListViewForScrollView;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.widget.SwipeLinearLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日程清单对话框
 * Created by liwei on 2017/4/18.
 */

public class CalenderNoteDialog extends Dialog {
    ExpandableListView expandableListView;
    String userID = "";
    String content = "";

    CallBack callBack;

    ExpandableListAdapter expandListAdapter;
    List<NoteTypeBean.ListBean> listBeenType = new ArrayList<>();
    List<List<NoteTitleDetailBean.TDelListBean>> listList = new ArrayList<>();

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;
    String maxTitleId;
    View headView;


    public CalenderNoteDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final String content, String maxTitleId) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        this.content = content;
        this.maxTitleId = maxTitleId;


        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_note, null);
        setContentView(view);

        expandableListView = (ExpandableListView) view.findViewById(R.id.listview);
        expandableListView.setGroupIndicator(null);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        headView = LayoutInflater.from(context).inflate(R.layout.add_note_header, null);
        expandableListView.addHeaderView(headView);
        expandableListView.addFooterView(LayoutInflater.from(context).inflate(R.layout.emptry_foolterview_white, null));

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userID = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERID, "0");
        listBeenType = App.getDBcApplication().getAllNoteTypeData(userID, false);

        if (listBeenType.size() == 0) {
            createNoteType();
        }
        if (listBeenType.size() > 1) {
            if (listBeenType.get(0).localId != -1 || listBeenType.get(1).localId != 0) {
                noteTypeBean(0, "备忘录", 0, 0);
//                noteTypeBean(-1, "收到的", -1, -1);
            }
        }
        loaddata();

        headView.findViewById(R.id.new_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String titleid = DateUtil.nowTime1();
                NoteTitleDetailBean.TDelListBean bean = new NoteTitleDetailBean.TDelListBean();
                bean.titleId = titleid;
                bean.uid = Integer.valueOf(userID);
                bean.id = App.getDBcApplication().getTiMinId() - 1;
                bean.styles = 1;
                bean.changeTime = DateUtil.nowTime();
                bean.createTime = DateUtil.nowTime();
                bean.localTimes = DateUtil.nowTime();
                bean.ltype = 0;
                bean.orderId = 0;
                bean.copys = "0";
                bean.filed = "0";
                bean.titles = "";
                bean.imgPath = "";
                bean.imgUrl = "";
                bean.nums = "";
                bean.shareUrl = "";
                bean.remark = "1";
                bean.remark1 = "0";
                bean.remark2 = "";
                bean.remark3 = "";
                bean.pname = "";
                bean.puid = 0;
                bean.states = 0;
                bean.sends = 0;
                bean.openState = 0;

                App.getDBcApplication().saveNoteTitleData(bean);
                App.getDBcApplication().updateNoteTitleState(bean.titleId, userID, 1, false);


                NoteTitleDetailBean.ListBean listBean = new NoteTitleDetailBean.ListBean();
                listBean.titleId = titleid;
                listBean.uid = Integer.valueOf(userID);
                listBean.id = App.getDBcApplication().getMinId() - 1;
                listBean.style = 1;
                listBean.changetime = DateUtil.nowTime();
                listBean.createTime = DateUtil.nowTime();
                listBean.lType = 0;
                listBean.orderId = 0;
                listBean.endstate = 0;
                listBean.remark = "";
                listBean.contents = content;
                listBean.titles = "";
                listBean.imgPath = "";
                listBean.imgUrl = "";
                listBean.cpath = "";
                listBean.curl = "";
                listBean.nums = "";
                listBean.shareUrl = "";
                listBean.remark1 = "";
                listBean.remark2 = "";
                listBean.remark3 = "";
                listBean.remark4 = "";
                App.getDBcApplication().saveNoteDetailData(listBean);
                App.getDBcApplication().updateNoteTitleDetailState(listBean.titleId, listBean.id, userID, 1, false);

                Toast.makeText(context, "新建成功", Toast.LENGTH_SHORT).show();

                Intent pull = new Intent();
                pull.setAction("pullRecevier");
                context.sendBroadcast(pull);

                dismiss();
            }
        });
    }

    private void createNoteType() {
        noteTypeBean(0, "备忘录", 0, 0);
//        noteTypeBean(-1, "收到的", -1, -1);
        noteTypeBean(1, "工作", 1, 1);
        noteTypeBean(2, "生活", 2, 2);
        noteTypeBean(3, "其他", 3, 3);
        noteTypeBean(4, "分类A", 4, 4);
        noteTypeBean(5, "分类B", 5, 5);
        noteTypeBean(6, "分类C", 6, 6);
    }

    public void loaddata() {
        listBeenType = App.getDBcApplication().getAllNoteTypeData(userID, false);
        List<NoteTypeBean.ListBean> nb = App.getDBcApplication().getAllNoteTypeData(userID, false);
        List<NoteTitleDetailBean.TDelListBean> db;
        List<List<NoteTitleDetailBean.TDelListBean>> nbd = new ArrayList<>();
        listList.clear();
//        if (listBeenType.size() > 1) {
//            NoteTypeBean.ListBean bean = listBeenType.get(0);
//            listBeenType.remove(0);
//            listBeenType.add(bean);
//            NoteTypeBean.ListBean bean1 = nb.get(0);
//            nb.remove(0);
//            nb.add(bean1);
//        }

        for (NoteTypeBean.ListBean bean : listBeenType) {
            db = App.getDBcApplication().getFiledNoteTitlesTypeDataDialog(userID, "0", bean.localId + "");

            Collections.sort(db, new Comparator<NoteTitleDetailBean.TDelListBean>() {
                @Override
                public int compare(NoteTitleDetailBean.TDelListBean lhs, NoteTitleDetailBean.TDelListBean rhs) {
                    return rhs.localTimes.compareTo(lhs.localTimes);
                }
            });
            if (!maxTitleId.isEmpty()) {
                for (NoteTitleDetailBean.TDelListBean dbbean : db) {
                    if (dbbean.titleId.equals(maxTitleId)) {
                        db.remove(dbbean);
                        break;
                    }
                }
            }
            if (bean.localId == -1){
                if (db.size() > 0){
                    for (NoteTitleDetailBean.TDelListBean bean1 : db){
                        if (listList.size() > 0){
                            listList.get(0).add(bean1);
                            nbd.get(0).add(bean1);
                        }else {
                            listList.add(db);
                            nbd.add(db);
                        }
                    }
                }
            } else {
                listList.add(db);
                nbd.add(db);
            }

        }
        if (listBeenType != null) {
            if (listBeenType.size() == 6) {
                db = App.getDBcApplication().getFiledNoteTitlesTypeDataDialog(userID, "0", "-1");

                Collections.sort(db, new Comparator<NoteTitleDetailBean.TDelListBean>() {
                    @Override
                    public int compare(NoteTitleDetailBean.TDelListBean lhs, NoteTitleDetailBean.TDelListBean rhs) {
                        return rhs.localTimes.compareTo(lhs.localTimes);
                    }
                });
                if (db.size() > 0){
                    for (NoteTitleDetailBean.TDelListBean bean1 : db){
                        listList.get(0).add(bean1);
                        nbd.get(0).add(bean1);
                    }
                }
            }
        }
        for (NoteTypeBean.ListBean listBean : nb){
            if (listBean.localId == -1){
                nb.remove(listBean);
                break;
            }
        }
        for (NoteTypeBean.ListBean listBean : listBeenType){
            if (listBean.localId == -1){
                listBeenType.remove(listBean);
                break;
            }
        }

        for (int i = 0; i < nb.size(); i++) {
            if (nbd.size() > 0) {
                if (nbd.get(i).size() == 0) {
                    listBeenType.remove(i - (nb.size() - listBeenType.size()));
                    listList.remove(i - (nbd.size() - listList.size()));
                }
            }
        }

//        listList.remove(listList.size() - 1);
//        listBeenType.remove(listBeenType.size() - 1);

        expandListAdapter = new NoteExpandListAdapter(context, listBeenType, listList);
        expandableListView.setAdapter(expandListAdapter);
        for (int i = 0; i < listBeenType.size(); i++) {
            expandableListView.expandGroup(i);
        }
    }

    private void noteTypeBean(int localId, String name, int orderId, int styleid) {
        NoteTypeBean.ListBean bean = new NoteTypeBean.ListBean();
        bean.id = 0;
        bean.uid = Integer.valueOf(userID);
        bean.content = name;
        bean.changeTime = DateUtil.formatDateTimeSs(new Date());
        bean.orderId = orderId;
        bean.styleId = styleid;
        bean.localId = localId;
        bean.remark = "";
        bean.remark1 = "";

        App.getDBcApplication().saveNoteTypeData(bean);
    }


    private void bwl() {

        NoteTitleDetailBean.TDelListBean bean = new NoteTitleDetailBean.TDelListBean();
        bean.titleId = "0";
        bean.uid = Integer.valueOf(userID);
        bean.id = -1;
        bean.styles = 1;
        bean.changeTime = DateUtil.nowTime();
        bean.createTime = DateUtil.nowTime();
        bean.localTimes = DateUtil.nowTime();
        bean.ltype = 0;
        bean.orderId = 0;
        bean.copys = "0";
        bean.filed = "0";
        bean.titles = "备忘录";
        bean.imgPath = "";
        bean.imgUrl = "";
        bean.nums = "";
        bean.shareUrl = "";
        bean.remark = "0";
        bean.remark1 = "0";
        bean.remark2 = "";
        bean.remark3 = "";
        bean.pname = "";
        bean.puid = 0;
        bean.states = 0;
        bean.sends = 0;
        bean.openState = 0;

        App.getDBcApplication().saveNoteTitleData(bean);
        App.getDBcApplication().updateNoteTitleState(bean.titleId, userID, 1, false);


        if (!App.getDBcApplication().getNoteTitleBuyNote(userID)) {
            NoteTitleDetailBean.TDelListBean bean1 = new NoteTitleDetailBean.TDelListBean();
            bean1.titleId = "1";
            bean1.uid = Integer.valueOf(userID);
            bean1.id = -2;
            bean1.styles = 0;
            bean1.changeTime = DateUtil.nowTime();
            bean1.createTime = DateUtil.nowTime();
            bean1.localTimes = DateUtil.nowTime();
            bean1.ltype = 1;
            bean1.orderId = 0;
            bean1.copys = "1";
            bean1.filed = "0";
            bean1.titles = "我的购物清单";
            bean1.imgPath = "";
            bean1.imgUrl = "";
            bean1.nums = "";
            bean1.shareUrl = "";
            bean1.remark = "0";
            bean1.remark1 = "0";
            bean1.remark2 = "";
            bean1.remark3 = "";
            bean1.pname = "";
            bean1.puid = 0;
            bean1.states = 0;
            bean1.sends = 0;
            bean1.openState = 1;

            App.getDBcApplication().saveNoteTitleData(bean1);
            App.getDBcApplication().updateNoteTitleState(bean1.titleId, userID, 1, false);
        }


        loaddata();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

    }

    public interface CallBack {
        void reminderEditTime();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


    class NoteExpandListAdapter extends BaseExpandableListAdapter {

        Context context;
        LayoutInflater layoutInflater;

        List<NoteTypeBean.ListBean> listList = new ArrayList<>();
        List<List<NoteTitleDetailBean.TDelListBean>> tdellist = new ArrayList<>();

        public NoteExpandListAdapter(Context context,
                                     List<NoteTypeBean.ListBean> listList,
                                     List<List<NoteTitleDetailBean.TDelListBean>> tdellist) {
            this.listList.clear();
            this.tdellist.clear();
            this.context = context;
            this.listList = listList;
            this.tdellist = tdellist;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getGroupCount() {
            return listList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return listList.size() == 0 ? 0 : tdellist.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }


        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            final GroupViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new GroupViewHolder();
                convertView = layoutInflater.inflate(R.layout.note_group_item, parent, false);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.typeimg = (ImageView) convertView.findViewById(R.id.typeimg);
                viewHolder.copy = (TextView) convertView.findViewById(R.id.copy);
                viewHolder.more = (ImageView) convertView.findViewById(R.id.more);
                viewHolder.emty = convertView.findViewById(R.id.emty);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (GroupViewHolder) convertView.getTag();
            }
            viewHolder.copy.setVisibility(View.GONE);
            viewHolder.more.setVisibility(View.GONE);
            if (tdellist != null && tdellist.size() > 0) {
                if (groupPosition == listList.size() - 1 || groupPosition == 0) {
                    if (tdellist.get(groupPosition).get(0).titleId == null) {
                        viewHolder.name.setText(listList.get(groupPosition).content + "(0)");
                    } else {
                        viewHolder.name.setText(listList.get(groupPosition).content + (tdellist.size() == 0 ? "(0)" : "(" + tdellist.get(groupPosition).size() + ")"));
                    }
                } else {
                    viewHolder.name.setText(listList.get(groupPosition).content + (tdellist.size() == 0 ? "(0)" : "(" + tdellist.get(groupPosition).size() + ")"));
                }
            } else {
                viewHolder.name.setText(listList.get(groupPosition).content + "(0)");
            }
            switch (listList.get(groupPosition).styleId) {
                case 0:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_beiwanglu);
                    break;
                case 1:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_gongzuo);
                    break;
                case 2:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_shenghuo);
                    break;
                case 3:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_fenlei);
                    break;
                case 4:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_fenlei);
                    break;
                case 5:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_fenlei);
                    break;
                case 6:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_fenlei);
                    break;
                case 7:
                    viewHolder.typeimg.setBackgroundResource(R.mipmap.icon_shoudao);
                    break;
            }
            viewHolder.emty.setBackgroundColor(Color.WHITE);

            return convertView;
        }


        class GroupViewHolder {
            TextView name, copy;
            View emty;
            ImageView typeimg, more;
        }


        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            final ChildViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ChildViewHolder();
                convertView = layoutInflater.inflate(R.layout.note_list_item, parent, false);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ChildViewHolder) convertView.getTag();
            }

            viewHolder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = App.getDBcApplication().getTitleNoteDetailData(userID, tdellist.get(groupPosition).get(childPosition).titleId, false).size();
                    NoteTitleDetailBean.ListBean listBean = new NoteTitleDetailBean.ListBean();
                    listBean.titleId = tdellist.get(groupPosition).get(childPosition).titleId;
                    listBean.uid = Integer.valueOf(userID);
                    listBean.id = App.getDBcApplication().getMinId() - 1;
                    listBean.style = tdellist.get(groupPosition).get(childPosition).styles;
                    listBean.changetime = DateUtil.nowTime();
                    listBean.createTime = DateUtil.nowTime();
                    listBean.lType = tdellist.get(groupPosition).get(childPosition).ltype;
                    listBean.orderId = num + 1;
                    listBean.endstate = 0;
                    listBean.remark = "";
                    listBean.contents = content;
                    listBean.titles = tdellist.get(groupPosition).get(childPosition).titles;
                    listBean.imgPath = "";
                    listBean.imgUrl = "";
                    listBean.cpath = "";
                    listBean.curl = "";
                    listBean.nums = "";
                    listBean.shareUrl = "";
                    listBean.remark1 = "";
                    listBean.remark2 = "";
                    listBean.remark3 = "";
                    listBean.remark4 = "";
                    App.getDBcApplication().saveNoteDetailData(listBean);
                    App.getDBcApplication().updateNoteTitleDetailState(listBean.titleId, listBean.id, userID, 1, false);

                    Toast.makeText(context, "加入成功", Toast.LENGTH_SHORT).show();

                    dismiss();

                }
            });

            if (tdellist.get(groupPosition).size() > 0) {
                if (tdellist.get(groupPosition).get(childPosition).titleId != null) {
                    int num = App.getDBcApplication().getTitleNoteDetailData(tdellist.get(groupPosition).get(childPosition).uid + "", tdellist.get(groupPosition).get(childPosition).titleId, false).size();
                    String titlestr;
                    if (tdellist.get(groupPosition).get(childPosition).titles.equals("")) {
                        titlestr = App.getDBcApplication().getNoteTitleDetailContent(tdellist.get(groupPosition).get(childPosition).uid + "", tdellist.get(groupPosition).get(childPosition).titleId);
                        if (titlestr.isEmpty()) {
                            titlestr = "无标题";
                        } else {
                            if (titlestr.length() > 12) {
                                titlestr = titlestr.substring(0, 12);
                            }
                        }

                    } else {
                        titlestr = tdellist.get(groupPosition).get(childPosition).titles;
                    }

                    String nums;
                    if (num == 0) {
                        if (tdellist.get(groupPosition).get(childPosition).titleId.equals("0")) {
                            titlestr = "<font color='#000000'>" + titlestr + "</>";//30a9a0
                        } else {
                            titlestr = "<font color='#000000'>" + titlestr + "</>";
                        }
                        viewHolder.title.setText(Html.fromHtml(titlestr));
                    } else {
                        String title_new;
                        if (tdellist.get(groupPosition).get(childPosition).styles == 0) {
                            int tick = App.getDBcApplication().getNoteDetailEndstateNums(tdellist.get(groupPosition).get(childPosition).titleId, tdellist.get(groupPosition).get(childPosition).uid + "");
                            if (num == 0) {
                                title_new = "";
                            } else {
                                title_new = titlestr + "(" + tick + "/" + num + ")";
                            }
                        }else if(tdellist.get(groupPosition).get(childPosition).styles == 4){
                            int tick1 = App.getDBcApplication().getNoteDetailEndstateNums(tdellist.get(groupPosition).get(childPosition).titleId, tdellist.get(groupPosition).get(childPosition).uid + "",1);
                            int tick2 = App.getDBcApplication().getNoteDetailEndstateNums(tdellist.get(groupPosition).get(childPosition).titleId, tdellist.get(groupPosition).get(childPosition).uid + "",2);
                            if (num == 0) {
                                title_new = "";
                            } else {
                                title_new = titlestr + "("+ tick1 + "/"+tick2+"/" + num +  ")";
                            }
                        } else {
                            if (num == 0) {
                                title_new = titlestr;
                            } else {
                                title_new = titlestr + "(" + num + ")";
                            }
                        }
                        setcolor(title_new, viewHolder.title);
                    }
                }
            }
            return convertView;
        }

        class ChildViewHolder {
            TextView title;
            LinearLayout ll;
        }


        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }


        //设置双#号包裹的文字颜色
        private void setcolor(String content, TextView editText) {
            SpannableStringBuilder builder = new SpannableStringBuilder(content);
            if (content.length() > 1) {
                List<Integer> integerList = new ArrayList<>();
                int index = 0;
                int count = hit(content, "#");
                if (count > 1) {
                    if (count % 2 == 0) {
                        for (int i = 0; i < count; i++) {
                            if (i == 0) {
                                index = content.indexOf("#");
                            } else {
                                index = content.indexOf("#", index + 1);
                            }
                            integerList.add(index);
                        }
                        for (int i = 0; i < integerList.size(); i++) {
                            if (i > 0) {
                                if (i % 2 != 0) {
                                    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLACK);
                                    builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                                }
                            }
                        }
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                        int khindex = content.indexOf("(");
                        builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        editText.setText(builder);
                    } else {
                        for (int i = 0; i < count - 1; i++) {
                            if (i == 0) {
                                index = content.indexOf("#");
                            } else {
                                index = content.indexOf("#", index + 1);
                            }
                            integerList.add(index);

                        }
                        for (int i = 0; i < integerList.size(); i++) {
                            if (i > 0) {
                                if (i % 2 != 0) {
                                    ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
                                    builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                                }
                            }
                        }
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                        int khindex = content.indexOf("(");
                        builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        editText.setText(builder);
                    }
                } else {
                    ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                    int khindex = content.indexOf("(");
                    builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    editText.setText(builder);
                }
            } else {
                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                int khindex = content.indexOf("(");
                builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                editText.setText(builder);
            }
        }

        public int hit(String a, String b) {
            if (a.length() < b.length()) {
                return 0;
            }
            char[] a_t = a.toCharArray();
            int count = 0;

            for (int i = 0; i < a.length(); i++) {
                StringBuffer buffer = new StringBuffer();
                for (int j = 0; j < b.length(); j++) {
                    buffer.append(a_t[i + j]);
                }
                if (buffer.toString().equals(b)) {
                    count++;
                }
            }

            return count;
        }
    }
}
